package picounit.loader;

import picounit.exception.ClassNotFound;
import picounit.exception.ClassUninstantiable;

public class Instantiator {
    public Object instantiate(String className) {
        return instantiate(fromName(className));
    }

    public <T> T instantiate(Class<T> clazz) {
        try {
            return clazz.newInstance();
        }
        catch (InstantiationException instantiationException) {
            throw new ClassUninstantiable(instantiationException);
        }
        catch (IllegalAccessException illegalAccessException) {
            throw new ClassUninstantiable(illegalAccessException);
        }
    }

    private Class<?> fromName(String className) {
        try {
            return Class.forName(className);
        }
        catch (ClassNotFoundException classNotFoundException) {
            throw new ClassNotFound(className);
        }
    }
}